package com.homegoods.videomodule.presenter;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.homegoods.aroutercomment.UserService;
import com.homegoods.classify.model.Constant;
import com.homegoods.videomodule.VideoApiUtils;
import com.homegoods.videomodule.model.CommentModel;
import com.homegoods.videomodule.model.SenderSidBean;
import com.homegoods.videomodule.ui.view.OnPublishCommentListener;
import com.kedll.library.mvp.AbstractRefreshPresenter;
import com.kedll.library.mvp.RefreshingView;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author QiQi
 * @ClassName: CommentPresenter
 * @date 2018/10/16
 * @description
 */
public class CommentPresenter extends AbstractRefreshPresenter<RefreshingView> {
    @Autowired
    public UserService userService;
    private int pos = 1;
    private String sid;

    public CommentPresenter(RefreshingView mView, String sid) {
        super(mView);
        this.sid = sid;
    }

    @Override
    protected void fetchData(boolean isLoadMore) {
        if (isLoadMore) {
            pos++;
        }
        VideoApiUtils.getVideoCommentList(sid, pos, subModelList -> {
            List<CommentModel> commentModels = JSON.parseArray(subModelList.getItems(), CommentModel.class);
            parserData(isLoadMore, commentModels);
        }, throwable -> {

        });


//        addSubscription(Observable.range(0, 10)
//                .map(integer -> {
//                    CommentModel commentModel = new CommentModel();
//                    commentModel.setComment("寻寻觅觅，冷冷清清，凄凄惨惨戚戚。乍暖还寒时候，最难将息。三杯两盏淡酒，怎敌他、晚来风急？雁过也，正伤心，却是旧时相识。满地黄花堆积。憔悴损，如今有谁堪摘？守着窗儿，独自怎生得黑？梧桐更兼细雨，到黄昏、点点滴滴。这次第，怎一个愁字了得！");
//                    commentModel.setGoodCount(String.valueOf(Constant.random.nextInt(10)));
//                    commentModel.setHasGood(Constant.random.nextInt(1));
//
////                    commentModel.setSenderSid(Constant.TEST_IMG_URL + Constant.random.nextInt());
////                    commentModel.setName("耳东陈");
//                    commentModel.setReplyCount(String.valueOf(Constant.random.nextInt(100)));
//                    commentModel.setTime(System.currentTimeMillis());
//                    return commentModel;
//                })
//                .toList()
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(commentModels -> ));
    }

    public void publishComment(String comment) {
        VideoApiUtils.publishComment(sid, comment, jsonObject -> {
            showCommentView(comment);
        }, throwable -> {

        });
    }

    private void showCommentView(String comment) {
        ARouter.getInstance().inject(this);
        addSubscription(Observable.just(comment)
                .map(s -> {
                    // FIXME: 2018/10/16 此处的发布人信息，获取当前登录用户的信息
                    CommentModel commentModel = new CommentModel();
                    commentModel.setComment(s);
                    commentModel.setGoodCount(String.valueOf(Constant.random.nextInt(10)));
                    commentModel.setHasGood(Constant.random.nextInt(1));
                    SenderSidBean senderSid = new SenderSidBean();
                    senderSid.setName(userService.getNiName());
                    senderSid.setHeadUrl(userService.getHeadImg());
                    commentModel.setReplyCount(String.valueOf(Constant.random.nextInt(100)));
                    commentModel.setTime(System.currentTimeMillis());
                    return commentModel.createItem(adapter);
                })
                .subscribe(commentItem -> {
                    adapter.addItem(0, commentItem);
                    adapter.notifyItemInserted(0);
                })
        );
    }
}
